package com.carworld.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.carworld.config.BaseController;
import com.carworld.model.DividendRecord;
import com.carworld.model.ShopOrder;
import com.carworld.model.SysUser;
import com.carworld.model.UserWithdrawal;
import com.jfinal.plugin.activerecord.Record;

public class WithdrawController extends BaseController {
    //跳转提现页面
    public void takeCashPage(){
        render("/pages/page_dividend/take_cash_page.html");
    }

	//跳转提现完成页面,新增提现记录
	public void takeCashToAd(){
		String userId=getUserCookie();
		BigDecimal tCashCount=UserWithdrawal.dao.findLastestByIdInCache(userId).get("withdrawls");//获取提现总数;
		BigDecimal divRecordCount=DividendRecord.dao.getDividendSum(userId).get("sumIncome");//分红总数
		BigDecimal ownCash=divRecordCount.subtract(tCashCount);//账户可提现余额

		String result = UserWithdrawal.dao.saveUserWithdrawal(getUserCookie(), getPara("mobile"),
				new BigDecimal(getPara("withdrawMoney")),getPara("taccount"),getParaToInt("takeType"),ownCash,getPara("takeName")
				,getPara("bankName"),getPara("bankAddress"),getPara("bankOwnerName"));
		System.out.println("提现result:" + result);
		JSONObject jsonObject = getJsonObject();
		jsonObject.put("result", result);
		renderJson(jsonObject);
	}

	public void returnWithdrawTakeCashResult(){
				render("/pages/page_dividend/withdrawTakeCashResult.html");
	}

    //获取提现余额
    public void getUserBalance(){
        String userId=getUserCookie();
        BigDecimal tCashCount=UserWithdrawal.dao.findLastestByIdInCache(userId).get("withdrawls");//获取提现总数;
		BigDecimal divRecordCount=DividendRecord.dao.getDividendSum(userId).get("sumIncome");//分红总数
		BigDecimal ownCash=divRecordCount.subtract(tCashCount);//账户可提现余额
        JSONObject jsonObject = getJsonObject();
        jsonObject.put("tCashCount", tCashCount);
        jsonObject.put("userBalance", ownCash);
        renderJson(jsonObject);
    }

	//提现详情列表初始化
	public void getCashLs(){

		String userId = getUserCookie();
		List<UserWithdrawal> userWithdrawalsList=UserWithdrawal.dao.findUserWithdrawalLsByCache(userId,0);
		String countNum=UserWithdrawal.dao.getCountWiByUserId(userId);
		JSONObject jsonObject = getJsonObject();
		jsonObject.put("withdrawals", userWithdrawalsList);
		jsonObject.put("countNum", countNum);
		renderJson(jsonObject);
	}
	//动态加载提现详情列表
	public void getCashLs2(){
		int indexNum = getParaToInt("indexNum");
		String userId = getUserCookie();
		List<UserWithdrawal> userWithdrawalsList=UserWithdrawal.dao.findUserWithdrawalLsByCache(userId,indexNum*15);
		indexNum=indexNum+1;
		JSONObject jsonObject = getJsonObject();
		jsonObject.put("withdrawals", userWithdrawalsList);
		jsonObject.put("indexNum", indexNum);
		renderJson(jsonObject);
	}

	/**
	 * 查看可提现余额(店铺营业余额、股东分红余额）
	 */
	public void getWithdrawalBalance() {
		String userId = getUserCookie();
		BigDecimal sumIncome = new BigDecimal(0.00);
		BigDecimal balance = new BigDecimal(0.00);
		Record withdraw = UserWithdrawal.dao.findLastRecordById(userId);
		SysUser user = SysUser.dao.findByIdInCache(userId);
		if (user != null && "1".equals(user.getUserRole())) {
			sumIncome = DividendRecord.dao.getDividendSum(userId).get("sumIncome");
			System.out.println("股东sumIncome：" + sumIncome);
		} else if (user != null && "2".equals(user.getUserRole())) {
			sumIncome = ShopOrder.dao.getShopIncomeSum(userId).get("sumIncome");
			System.out.println("sumIncome：" + sumIncome);
		} else {
			System.out.println("用户不存在");
		}

		System.out.println(withdraw.toString());
		if (null != withdraw.getBigDecimal("withdrawls")) {
			balance = sumIncome.subtract(withdraw.getBigDecimal("withdrawls"));
			System.out.println("之前有提现，金额为：" + withdraw.getBigDecimal("withdrawls"));
		} else {
			balance = sumIncome;
			System.out.println("之前无提现");
		}
		System.out.println("账户余额为：" + balance);
		renderJson(balance);
	}

	/**
	 * 保存提现记录
	 */
	public void saveWithdrawalRecord() {
		String result = UserWithdrawal.dao.saveUserWithdrawal(getUserCookie(), getPara("mobile"),
				new BigDecimal(getPara("money")), new BigDecimal(getPara("balance")), getPara("account"));
		System.out.println("提现result:" + result);
		renderText(result);
	}

}
